/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014-6-24         luwei
 */

package com.tianci.appstore.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;

import com.tianci.appstore.ui.widget.OnButtonClickListener;
import com.tianci.appstore.utils.SkyConstants;

public class BaseDialog extends Dialog implements OnClickListener {
    protected Context mContext = null;
    protected FrameLayout mLayout = null;
    private OnButtonClickListener mClickListener = null;
    public static final int NAME_SIZE = SkyConstants.Dpi(40);
    public static final int OTHER_SIZE = SkyConstants.Dpi(36);
    protected boolean mResponseBackKey = true;

    public BaseDialog(Context context) {
        super(context);
        this.mContext = context;
        mLayout = new FrameLayout(context);
    }

    public void setButtonClickListener(OnButtonClickListener listener) {
        this.mClickListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(mLayout);
        getWindow().getAttributes().dimAmount = 0.65f;
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    }

    public void setResponseBackKey(boolean response) {
        this.mResponseBackKey = response;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (!mResponseBackKey) {
                        return true;
                    }
                    break;

                default:
                    break;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        if (mClickListener != null) {
            mClickListener.onClick((Integer) v.getTag());
        }
    }

}
